﻿(function (wtm) {
    wtm.transfer_Render = function (para) {
        if (para.beforeMount.length > 0) {
            eval(para.beforeMount + '(para);');
        }

        if (para.url.length > 0) {
            $.get(para.url, function (resp) {
                para.data = resp.Data;

                wtm.transfer_render(para);

            });
        }
        else {
            wtm.transfer_render(para);
        }

    };

    wtm.transfer_render = function (para) {
        var $el = $('#' + para.id);
        var $div = $('#div' + para.id);

        layui.transfer.render({
            elem: '#div' + para.id,
            data: para.data,
            value: para.value,
            id: para.id,
            showSearch: para.showSearch,
            width: para.width,
            height: para.height,
            text: {
                none: '无数据' //没有数据时的文案
                , searchNone: '无匹配数据' //搜索无匹配数据时的文案
            },
            parseData: function (res) {
                return {
                    "value": res[para.valueKey] //数据值
                    , "title": res[para.textKey] //数据标题
                    , "disabled": res.Disabled  //是否禁用
                    , "checked": res.Selected //是否选中
                }
            },
            onchange: function (data, index) {
                console.log(data);
                //删除wtm-field
                $div.find('input[wtm-field]').remove();
                $el.val('');

                var selectArr = layui.transfer.getData(para.id);
                //冲销添加wtm-field
                for (var i = 0; i < selectArr.length; i++) {
                    $div.append('<input wtm-field type="hidden" name="' + para.fieldName + '" value="' + selectArr[i].value + '"/>');
                    $el.val('hasValue');
                }

            }
        });


        //添加初值表单

        for (var i = 0; i < para.value.length; i++) {

            $div.append('<input wtm-field type="hidden" name="' + para.fieldName + '" value="' + para.value[i] + '"/>');
            $el.val("hasValue");
        }

        if (para.mounted.length > 0) {
            eval(para.mounted + '(para);');
        }

        $(window).resize();
    }
})(wtm);